package com.daka.service.impl;

import com.daka.dao.UserMapper;
import com.daka.entity.User;
import com.daka.entity.Volunteer;
import com.daka.service.UserService;
import com.daka.service.VolunteerService;
import com.daka.service.qnzyz.BHMemberService;
import com.daka.tools.DKQConstant;
import com.daka.tools.MD5Util;
import com.daka.tools.UUIDHexGenerator;
import com.daka.tools.rongYun.RongCloudSMS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:24
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VolunteerService volunteerService;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private BHMemberService bhMemberService;



    public User queryByMobile(String mobile){
        User user = userMapper.queryByMobile(mobile);
        return user;
    }

    public User selectByPrimaryKey(long id){
        return userMapper.selectByPrimaryKey(id);
    }

    public int insert(User user){

        user.setEmail(user.getMobile() + "@lifeyoyo.com");
        user.setPlatform(2);
        user.setOldid(0L);
        user.setLocked(0);
        user.setLat(Double.valueOf(0));
        user.setLng(Double.valueOf(0));
        user.setPassword(MD5Util.MD5(user.getPassword()));
        user.setRoles("user");
        user.setCreateTime(new Timestamp(new Date().getTime()));
        if (user.getNickName() == null) {
            user.setRealName(user.getNickName());
        }

        try
        {
            //提交北航
            reg2BHPlatforpUser(user);
        } catch (Exception e)
        {
            e.printStackTrace();
            try
            {
                throw e;
            } catch (Exception e1)
            {
                e1.printStackTrace();
            }
        }

        //保存用户
        long a =userMapper.insert(user);
        System.out.println(a);
        //保存志愿者
        Volunteer volunteer =new Volunteer();
        volunteer.setUserId(user.getId());
        volunteer.setMemberCode(UUIDHexGenerator.generator());
        volunteer.setCrateTime(new Timestamp(new Date().getTime()));
        volunteer.setMobile(user.getMobile());
        volunteer.setNickName(user.getNickName());
        volunteer.setRealName(user.getRealName());
        volunteer.setUserId(user.getId());
        volunteer.setHeadUrl(DKQConstant.HEAR_URL);
        volunteer.setTimes(0);
        volunteer.setLocked(0);
        volunteer.setSex("男");
        volunteer.setIsFinished(0);
        volunteer.setvTimes(0l);
        volunteer.setActs(0);
        volunteer.setRank(0);
        int b =volunteerService.insert(volunteer);
        if(b>0){
            return 1;
        }else{
            return 0;
        }
    }
    private void reg2BHPlatforpUser(User user) throws Exception
    {
        if (null != user.getMobile() && StringUtils.isNotBlank(user.getMobile()))
            bhMemberService.reg4Platform(user.getMobile(), user.getPassword());
    }

    public int update(User user){
        return userMapper.updateByPrimaryKeySelective(user);
    }


    public String sendVerifierCode(String mobile){
        String randCode = "";
        try{
            randCode = RongCloudSMS.sendCode(mobile);
            System.out.println(randCode);
            redisClient.set("resetPassword",mobile, randCode);
            redisClient.expire(mobile, 60);
        } catch (Exception e){
            e.printStackTrace();
        }
        return randCode;
    }

    public Boolean checkVerifierCode(String mobile,String code){
        try {
            String redisCode = (String)redisClient.get("resetPassword", mobile,String.class);
            if(redisCode != null && code.equals(redisCode)){
                redisClient.remove("resetPassword", mobile);
                return true;
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    private static String get4Rand(){
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return String.valueOf(tmp % (9999 - 1000 + 1) + 1000);
    }
}
